<!-- select2 -->
<%@ page contentType="text/html;charset=UTF-8" %>
<script type="text/javascript" src="<%=request.getContextPath()%>/static/BJUI/js/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/static/BJUI/plugins/select2/js/select2.full.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/static/BJUI/plugins/bootstrap.min.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/static/BJUI/plugins/select2/js/prettify.min.js"></script>
<link href="<%=request.getContextPath()%>/static/BJUI/themes/css/bootstrap.min.css" type="text/css" rel="stylesheet" />
<link href="<%=request.getContextPath()%>/static/BJUI/plugins/select2/css/select2.min.css" type="text/css" rel="stylesheet" />
test
 <select class="select2  form-control" multiple="multiple" data-url="<%=request.getContextPath()%>/select2.do" data-initvalue="1" >
 </select>
<script type="text/javascript">
$(".select2").each(function(i,j){
	var url=$(this).attr("data-url");
	var k=$(this).attr("data-initvalue");
	if(url){
		getCacheValue(this,url,k);
	}
	
});
function processData(obj,result,k){
		var data = result;
		$(obj).select2({
			placeholder: "请输入",
			initSelection: function(element, callback) { // 初始化时设置默认值  
				var initData=[];
				if(k!=''){
					$(data).each(function (idx,item) {
			           if(item.id==k){
			        	   initData.push({ id: item.id, text: item.text });
			           }
			        });
				}
				callback(initData);
			},
			data:data
			/*  ajax: {
				    url: url,
				    dataType: 'json',
				    data: function (params) {
			            return {
			                q: params.term
			            };
			        },
			        processResults: function (data, page) {
			            return { results: data };
			        },
				    cache: "true"
				  } */
				});
	
}
function getCacheValue(obj,url,k){
	$.getJSON(url,"id="+k,
			function(result){
				processData(obj,result,k);
	  		});
	
}
</script> 